package com.travel.server.manager.impl.controller.youzan;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.impl.youzan.MsgPushEntity;
import com.travel.server.manager.model.OrderInfo;
import com.youzan.open.sdk.client.oauth.model.OAuthToken;
import com.youzan.open.sdk.gen.v4_0_0.model.YouzanTradeGetResult;
import com.youzan.open.sdk.util.json.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/26 18:28
 * @Desc:
 */
@Controller
@Slf4j
public class PushController {

    private static final int mode = 1 ;
    private static final String clientId="TEST";
    private static final String clientSecret="TEST";

    @Autowired
    private OrderDal orderDal;

    @RequestMapping(value = "/test", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public Object test(@RequestBody MsgPushEntity entity) {
        JSONObject res = new JSONObject();
        res.put("code", 0);
        res.put("msg", "success");

        /**
         *  判断是否为心跳检查消息，1.是则直接返回
         */
        if (entity.isTest()) {
            return res;
        }

        /**
         * 解析消息推送的模式  这步判断可以省略
         * 0-商家自由消息推送 1-服务商消息推送
         * 以服务商举例,判断是否为服务商类型的消息,否则直接返回
         */
        if (entity.getMode() != mode ){
            return res;
        }


        /**
         * 对于msg 先进行URI解码
         */
        String msg;
        try {
            msg= URLDecoder.decode(entity.getMsg(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            log.error("解析msg异常：", e);
            return res;
        }

        /**
         *  接下来是一些业务处理
         *  判断当前消息的类型 比如交易
         */
        String type = entity.getType();
        if ("trade_TradeCreate".equals(type)) {
            YouzanTradeGetResult result = JsonUtils.toBean(msg, new TypeReference<YouzanTradeGetResult>() {});
        }else if("trade_TradePaid".equals(type)){
            YouzanTradeGetResult result = JsonUtils.toBean(msg, new TypeReference<YouzanTradeGetResult>() {});
        }else if("trade_TradeBuyerPay".equals(type)){
            YouzanTradeGetResult result = JsonUtils.toBean(msg, new TypeReference<YouzanTradeGetResult>() {});
        }else if("trade_TradePartlySellerShip".equals(type)){
            YouzanTradeGetResult result = JsonUtils.toBean(msg, new TypeReference<YouzanTradeGetResult>() {});
        }else if("trade_TradeSellerShip".equals(type)){
            YouzanTradeGetResult result = JsonUtils.toBean(msg, new TypeReference<YouzanTradeGetResult>() {});
        }else if("trade_TradeSuccess".equals(type)){
            YouzanTradeGetResult result = JsonUtils.toBean(msg, new TypeReference<YouzanTradeGetResult>() {});
        }else if("trade_TradeClose".equals(type)){
            YouzanTradeGetResult result = JsonUtils.toBean(msg, new TypeReference<YouzanTradeGetResult>() {});
        }
        return res;
    }

    private OrderInfo getOrderInfo(YouzanTradeGetResult result){
        String orderId =result.getFullOrderInfo().getOrderInfo().getTid();
        return orderDal.selectByOrderId(orderId);
    }

    private OrderInfo createOrder(YouzanTradeGetResult result){
        return null;
    }
}
